#include "netdisk.h"
int cdFunc(const char* arg, Stack_t* dir)
{

    char path[4096];
    onePath(arg, path,dir);
    printf("path_cd = %s\n", path);
    if (access(path, F_OK) == -1) {
        perror("access");
        return -1;
    }
    char argv[4096];
    strcpy(argv,arg);
    // printf("cd_onepath=%s\n",path);
    if (arg[0] == '/') {
        for (int i = 0; i < dir->stackSize - 1; i++) {
            char str[256];
            pop(dir, str);
        }
        travel_stack(*dir,argv);
        // printf("argv = %s\n",argv);
        char* p;
        strtok(path, "/");
        strtok(NULL, "/");
        while ((p = strtok(NULL, "/")) != NULL) {
            push(dir, p);
        }
    } else if (arg[0] == '.' && arg[1] == '.') {
        if (dir->stackSize == 1) {
            return 0;
        }
        char str[256] = {0};
        pop(dir, str);
        char* p;
        if ((p = strtok(argv, "/")) == NULL){
            // printf("argv = %s\n",argv);
            return 0;

        }


        while ((p = strtok(NULL, "/")) != NULL) {
            // printf("p = %s\n",p);
            push(dir, p);
        }
    } else {
        if (arg[0] == '.' && arg[1] == '\0' || arg[0] == '\0'){
            return 0;
        }

        char* p;
        p=strtok(argv,"/");
        if(p == NULL){
            return 0;
        }
        if(p[0]!='.')
        push(dir,p);
        while ((p = strtok(NULL, "/")) != NULL) {
            push(dir, p);
        }
    }
}
